﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;
using MyCommerce.Business.GeneralParameters;

namespace MyCommerce.Business.Stores
{
    public class ProductPriceManager
    { 
        private MyCommerceEntities vContext;

        public ProductPriceManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public ProductPriceManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public List<ProductPrice> GetProductPrices()
        {
            List<ProductPrice> colProduct = new List<ProductPrice>();

            var result = from st in vContext.ProductPrice
                         .Include("Product")
                         .Include("Classifier")
                         .Include("Supplier")
                         select st;
            if (result != null)
                colProduct = new List<ProductPrice>(result.AsEnumerable<ProductPrice>());

            return colProduct;
        }
        
        public ProductPrice GetProductPriceByID(long IdProductPrice)
        {
            ProductPrice objProduct = new ProductPrice();
            var result = (from st in vContext.ProductPrice
                          where st.IdProductPrice == IdProductPrice
                          select st).First();
            objProduct = (ProductPrice)result;
            return objProduct;

        }

        public ProductPrice GetProductPriceByProductAndMoneyAndSupplier(long idProduct, long idMoney, long idSupplier)
        {
            var result = (from st in vContext.ProductPrice
                         where st.IdProduct == idProduct && st.IsCurrentPrice == true && st.IdSupplier == idSupplier
                         select st).FirstOrDefault();
            if (result != null)
            {
                if (result.IdcMoney != idMoney)
                {
                    CurrencyExchangeManager exchangeMgr = new CurrencyExchangeManager();
                    CurrencyExchange objExchange = new CurrencyExchange();
                    objExchange.IdMoneyIncome = result.IdcMoney;
                    objExchange.IdMoneyExit = idMoney;
                    objExchange.AmmountIncome = result.PurchasePrice;

                    CurrencyExchange objResult = exchangeMgr.CurrencyExchangeGetExchange(objExchange);
                    if (objResult != null)
                        result.PurchasePrice = objResult.AmmountExit;

                    objExchange = new CurrencyExchange();
                    objExchange.IdMoneyIncome = result.IdcMoney;
                    objExchange.IdMoneyExit = idMoney;
                    objExchange.AmmountIncome = result.OtherPurchasePrice;

                    objResult = exchangeMgr.CurrencyExchangeGetExchange(objExchange);
                    if (objResult != null)
                        result.OtherPurchasePrice = objResult.AmmountExit;
                }
                return result;
            }
            return null;
        }

        public ProductPrice GetProductPriceByProductAndSupplier(long idProduct, long idSupplier)
        {
            var result = from st in vContext.ProductPrice
                         .Include("Product")
                         .Include("Classifier")
                         .Include("Supplier")
                         where st.IdProduct == idProduct 
                         && st.IsCurrentPrice == true && st.IdSupplier == idSupplier
                         select st;
            if (result != null)
            {
                List<ProductPrice> colPrices = new List<ProductPrice>(result.AsEnumerable<ProductPrice>());
                if (colPrices.Count > 0)
                    return colPrices[0];
            }
            return null;
        }

        public ProductPrice GetProductPriceByProduct(long idProduct)
        {
            var result = from st in vContext.ProductPrice
                         .Include("Product")
                         .Include("Classifier")
                         where st.IdProduct == idProduct
                         && st.IsCurrentPrice == true
                         select st;
            if (result != null)
            {
                List<ProductPrice> colPrices = new List<ProductPrice>(result.AsEnumerable<ProductPrice>());
                if (colPrices.Count > 0)
                    return colPrices[0];
            }
            return null;
        }

        public void ChangeProductPrice(ProductPrice objProductPrice)
        {
            var vDisableResult = from st in vContext.ProductPrice
                                 where st.IdProduct == objProductPrice.IdProduct
                                 select st;
            if (vDisableResult != null)
            {
                foreach (ProductPrice item in vDisableResult)
                {
                    item.IsCurrentPrice = false;
                }
            }            

            var vResult = (from st in vContext.ProductPrice
                           where st.IdProductPrice == objProductPrice.IdProductPrice
                           select st).First();            
            vResult.IsCurrentPrice = true;

            vContext.SaveChanges();
        }

        public void SaveProductPrice(ProductPrice objProductPrice)
        {
            //var validateResult = (from cl in vContext.ProductPrice
            //                      where (cl.IsCurrentPrice == true && cl.IdProduct == objProductPrice.IdProduct)
            //                      select cl).Count();
            //if (validateResult > 0)
            //{
            //    throw new Exception("Ya existe un precio actual para este producto. Primero deshabilite el precio anterior y luego vuelva a habilitar el actual.");
            //}

            vContext.AddToProductPrice(objProductPrice);
            vContext.SaveChanges();

            ChangeProductPrice(objProductPrice);
        }

        public void UpdateProductPrice(ProductPrice objProductPrice)
        {
            var validateResult = (from cl in vContext.ProductPrice
                                  where (cl.IsCurrentPrice == true && cl.IdProduct == objProductPrice.IdProduct && cl.IdProductPrice != objProductPrice.IdProductPrice)
                                  select cl).Count();
            if (validateResult > 0)
            {
                throw new Exception("Ya existe un precio actual para este producto. Primero deshabilite el precio anterior y luego vuelva a habilitar el actual.");
            }

            var vResult = (from st in vContext.ProductPrice
                           where st.IdProductPrice == objProductPrice.IdProductPrice
                           select st).First();


            vResult.IdProduct = objProductPrice.IdProduct;
            vResult.PurchasePrice = objProductPrice.PurchasePrice;
            vResult.SalePrice = objProductPrice.SalePrice;
            vResult.OtherPurchasePrice = objProductPrice.OtherPurchasePrice;
            vResult.IdcMoney = objProductPrice.IdcMoney;
            vResult.IdSupplier = objProductPrice.IdSupplier;
            vResult.IsCurrentPrice = objProductPrice.IsCurrentPrice;
            vResult.RegisterDate = objProductPrice.RegisterDate;            

            vContext.SaveChanges();
        }

        public void DeleteProductPrice(long IdProductPrice)
        {
            var vResult = (from st in vContext.ProductPrice
                           where st.IdProductPrice == IdProductPrice
                           select st).First();
            vContext.DeleteObject(vResult);
            vContext.SaveChanges();
        }

    }
}
